package com.changwu.cwbase.ui.commont;

import android.os.Bundle;
import android.os.Vibrator;

import com.changwu.cwbase.R;
import com.changwu.cwbase.base.ActivityBase;
import com.changwu.cwbase.util.LogUtil;

import butterknife.BindView;
import cn.bingoogolapple.qrcode.core.QRCodeView;
import cn.bingoogolapple.qrcode.zxing.ZXingView;

/*
 * 通用使用模板
 */
public class CommonScanActivity extends ActivityBase implements QRCodeView.Delegate {

    @BindView(R.id.zxv)
    ZXingView zxv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan_qr);
        initView();
    }

    @Override
    public void initToolBar() {
        super.initToolBar();
        title.setText("扫一扫");
    }

    private void initView() {
        zxv.setDelegate(this);
        try {
            if (zxv != null) {
                zxv.startSpot();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void onStart() {
        super.onStart();
        zxv.startCamera();
        zxv.startSpotAndShowRect(); // 显示扫描框，并开始识别
//        mQRCodeView.startCamera(Camera.CameraInfo.CAMERA_FACING_FRONT);
    }

    @Override
    protected void onStop() {
        zxv.stopCamera();
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        zxv.onDestroy();
        super.onDestroy();
    }


    @Override
    public void onScanQRCodeSuccess(String result) {
        try {
            LogUtil.i(TAG, "result:" + result);
            vibrate();
            zxv.postDelayed(new Runnable() {
                @Override
                public void run() {
                    zxv.startSpot();
                }
            }, 1500);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void vibrate() {
        Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
        vibrator.vibrate(200);
    }

    @Override
    public void onCameraAmbientBrightnessChanged(boolean isDark) {
        // 这里是通过修改提示文案来展示环境是否过暗的状态，接入方也可以根据 isDark 的值来实现其他交互效果
        String tipText = zxv.getScanBoxView().getTipText();
        String ambientBrightnessTip = "\n环境过暗，请打开闪光灯";
        if (isDark) {
            if (!tipText.contains(ambientBrightnessTip)) {
                zxv.getScanBoxView().setTipText(tipText + ambientBrightnessTip);
            }
        } else {
            if (tipText.contains(ambientBrightnessTip)) {
                tipText = tipText.substring(0, tipText.indexOf(ambientBrightnessTip));
                zxv.getScanBoxView().setTipText(tipText);
            }
        }
    }

    @Override
    public void onScanQRCodeOpenCameraError() {
        LogUtil.e("***", "打开相机出错");
    }

   /* @OnClick({R.id.back})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
        }
    }*/
}
